<template>
  <div class="app-container">
    <!-- <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="75px"
    >
      <el-form-item label="" prop="useTo">
        <el-input
          v-model="queryParams.useTo"
          placeholder="请输入用途"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="useToName">
        <el-input
          v-model="queryParams.useToName"
          placeholder="请输入部门/渠道"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="director">
        <el-input
          v-model="queryParams.director"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="returnName">
        <el-input
          v-model="queryParams.returnName"
          placeholder="请输入收件姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="returnPhone">
        <el-input
          v-model="queryParams.returnPhone"
          placeholder="请输入收件号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="returnAddress">
        <el-input
          v-model="queryParams.returnAddress"
          placeholder="请输入收件地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="" prop="returnRemark">
        <el-input
          v-model="queryParams.returnRemark"
          placeholder="请输入售后备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sample:returnAddress:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sample:returnAddress:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sample:returnAddress:remove']"
          >删除</el-button
        >
      </el-col> 
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sample:returnAddress:export']"
          >导出</el-button
        >
        
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['sample:returnAddress:list']"
          type="primary"
          size="mini"
          @click="openReturnAddress = true"
          >地址配置</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="returnAddressList"
      @selection-change="handleSelectionChange"
    >
      <!-- @row-dblclick="t" -->
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="用途" align="center" prop="useTo" />
      <el-table-column label="部门/渠道" align="center" prop="useToName" />
      <el-table-column label="负责人" align="center" prop="director" />
      <el-table-column label="收件姓名" align="center" prop="returnName" />
      <el-table-column label="收件号码" align="center" prop="returnPhone" />
      <el-table-column
        label="收件地址"
        align="center"
        prop="returnAddress"
        width="300"
      />
      <el-table-column label="售后备注" align="center" prop="returnRemark" />
      <!-- <el-table-column label="部门权限" align="center" prop="returnRemark">
      </el-table-column> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <!-- <el-table-column
        label="部门权限"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope" v-if="scope.row.roleId !== 1">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateBI(scope.row)"
            v-hasPermi="['system:role:edit']"
            >修改</el-button
          >
        </template>
      </el-table-column>
      
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sample:returnAddress:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sample:returnAddress:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改样衣退货地址对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用途" prop="useTo">
          <el-input v-model="form.useTo" placeholder="请输入用途" />
        </el-form-item>
        <el-form-item label="部门/渠道" prop="useToName">
          <el-input v-model="form.useToName" placeholder="请输入部门/渠道" />
        </el-form-item>
        <el-form-item label="负责人" prop="director">
          <el-input v-model="form.director" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="收件姓名" prop="returnName">
          <el-input v-model="form.returnName" placeholder="请输入收件姓名" />
        </el-form-item>
        <el-form-item label="收件号码" prop="returnPhone">
          <el-input v-model="form.returnPhone" placeholder="请输入收件号码" />
        </el-form-item>
        <el-form-item label="收件地址" prop="returnAddress">
          <!-- <el-input v-model="form.returnAddress" placeholder="请输入收件地址" /> -->
          <el-input
            v-model="form.returnAddress"
            placeholder="请输入收件地址"
            type="textarea"
            :autosize="{ minRows: 1, maxRows: 12 }"
          />
        </el-form-item>
        <el-form-item label="售后备注" prop="returnRemark">
          <el-input v-model="form.returnRemark" placeholder="请输入售后备注" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <!-- <el-form-item label="逻辑删除;0=未删除；1=已删除" prop="isDelete">
          <el-input v-model="form.isDelete" placeholder="请输入逻辑删除;0=未删除；1=已删除" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="修改BI权限配置"
      :visible.sync="openBi"
      width="300px"
      append-to-body
      v-if="openBi"
    >
      <el-row :gutter="20">
        <el-col :span="24" :xs="24">
          <el-checkbox
            v-model="menuNodeAll"
            @change="handleCheckedTreeNodeAll($event, 'designShootingSample')"
            >全选/全不选</el-checkbox
          >
          <div class="head-container el-dialog-div">
            <el-tree
              ref="Bi"
              node-key="deptId"
              :data="BiOptions"
              :props="defaultPropsBi"
              :expand-on-click-node="false"
              :default-expand-all="true"
              highlight-current
              show-checkbox
            />
          </div>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer" style="margin-top: 50px">
        <el-button type="primary" @click="submitFormBi">确 定</el-button>
        <el-button @click="cancelBi">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      append-to-body
      :visible.sync="openReturnAddress"
      width="1800px"
      v-if="openReturnAddress"
    >
      <returnAddress />
    </el-dialog>
  </div>
</template>

<script>
import returnAddress from "../returnAddress/index.vue";
import {
  listReturnAddress,
  getReturnAddress,
  delReturnAddress,
  addReturnAddress,
  updateReturnAddress,
  getPermissionList,
  editPermission,
  getDeptPermissionList,
} from "@/api/sample/returnAddress";

export default {
  name: "ReturnAddress",
  components: {
    returnAddress,
  },
  data() {
    return {
      openReturnAddress: false,
      //全选/全不选
      menuNodeAll: false,
      defaultPropsBi: {
        children: "children",
        label: "deptName",
      },
      formBi: {},
      //
      openBi: false,
      //Bi树
      BiOptions: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 样衣退货地址表格数据
      returnAddressList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        useTo: null,
        useToName: null,
        director: null,
        returnName: null,
        returnPhone: null,
        returnAddress: null,
        returnRemark: null,
        isDelete: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        returnPhone: [
          { required: true, message: "收件号码不能为空", trigger: "blur" },
        ],
        returnName: [
          { required: true, message: "收件姓名不能为空", trigger: "blur" },
        ],
        returnAddress: [
          { required: true, message: "收件地址不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    //点击某一行
    t(row, column, cell, event) {
      console.log(row, column, cell, event);
      this.handleUpdate(row);
    },
    /** 查询样衣退货地址列表 */
    getList() {
      this.loading = true;
      getDeptPermissionList(this.queryParams).then((response) => {
        this.returnAddressList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        useTo: null,
        useToName: null,
        director: null,
        returnName: null,
        returnPhone: null,
        returnAddress: null,
        returnRemark: null,
        remark: null,
        isDelete: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加样衣退货地址";
    },
    // 树权限（全选/全不选）
    handleCheckedTreeNodeAll(value, type) {
      console.log(JSON.stringify(value));
      if (type == "designShootingSample") {
        this.$refs.Bi.setCheckedNodes(value ? this.BiOptions : []);
      }
    },
    cancelBi() {
      this.openBi = false;
    },
    submitFormBi() {
      let array = this.getMenuAllCheckedKeysBi();
      this.formBi.permisstionList = array.filter((item) => item !== null);
      // this.formBi.t4Ids = this.getMenuAllCheckedKeysBi();
      editPermission(this.formBi).then((response) => {
        if (response.code == 200) {
          this.$modal.msgSuccess("配置成功");
        } else {
          this.$modal.msgError("配置失败");
        }

        this.openBi = false;
        this.getList();
      });
    },
    // 所有菜单节点数据
    getMenuAllCheckedKeysBi() {
      // 目前被选中的菜单节点
      let checkedKeys = this.$refs.Bi.getCheckedKeys();
      // 半选中的菜单节点
      let halfCheckedKeys = this.$refs.Bi.getHalfCheckedKeys();
      checkedKeys.unshift.apply(checkedKeys, halfCheckedKeys);
      return checkedKeys;
    },
    /** 根据角色ID查询BI树结构 */
    getRoleBiTreeselect(roleId) {
      return getPermissionList(roleId).then((response) => {
        this.BiOptions = response.data.sysDept;
        return response;
      });
    },
    /** 修改按钮操作 */
    handleUpdateBI(row) {
      this.reset();
      const id = row.id || this.ids;
      // this.formBi.roleId = row.roleId;
      const roleMenu = this.getRoleBiTreeselect(id);
      this.formBi.returnID = id;
      // return
      // getPermissionList(id).then((response) => {
      // this.formBi.roleName = response.data.roleName;
      // this.formBi.roleKey = response.data.roleKey;
      this.openBi = true;
      this.$nextTick(() => {
        roleMenu.then((res) => {
          let checkedKeys = res.data.checkedKeys;
          console.log(JSON.stringify(checkedKeys));
          checkedKeys.forEach((v) => {
            this.$nextTick(() => {
              this.$refs.Bi.setChecked(v, true, false);
            });
          });
        });
      });
      this.title = "修改BI权限配置";
      // });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getReturnAddress(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改样衣退货地址";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateReturnAddress(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReturnAddress(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除样衣退货地址编号为"' + ids + '"的数据项？')
        .then(function () {
          return delReturnAddress(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "sample/returnAddress/export",
        {
          ...this.queryParams,
        },
        `returnAddress_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
